#include "sorftime_app.h"

#include "stdio.h"

/***********计数值**************/
#define ST_START_COUNT_VALUE 0
#define ST_END_COUNT_VALUE 0xFFFFFFFE
#define ST_ID_NULL 0
/************ID值****************/
#define ST_NULL_ID 0
#define ST_NEW_ID 1
#define ST_OLD_ID 2

static s_timer_id timer_id_ary;

static uint8_t OPEN_COUNTER = 0;
static uint8_t MAX_USE_NUMBER = 0;

#ifdef ST_CLIENT_SERVER_MODE
static s_event_id event_id_ary;
static void add_event_to_list(st_rtn_func_t event_cb, uint16_t id);
#endif

/**
 * @brief 定时器中断回调，对所有计数值进行减少
 *       中断触发为 1ms
 * 		根据用户层需要的计时时间,到达就改变状态
 **/
void st_sorft_timer_counter_prosess(void)
{ /* 此函数放入中断回调*/
	uint8_t i = 0;
	st_id_t end_id;

	if (OPEN_COUNTER)
	{
		for (i = 0; i < MAX_USE_NUMBER; i++)
		{
			if ((timer_id_ary.id_ary[i].id) != ST_ID_NULL)
			{
				if ((timer_id_ary.id_ary[i].run_state != TIM_SUSPEND) && (timer_id_ary.id_ary[i].run_state != TIM_END))
				{
					if ((++(timer_id_ary.id_ary[i].count)) >= (timer_id_ary.id_ary[i].target))
					{
						timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
						end_id.id = timer_id_ary.id_ary[i].id;
						end_id.run_state = timer_id_ary.id_ary[i].run_state;
						end_id.end_state = timer_id_ary.id_ary[i].end_state;
						end_id.rtn_func = timer_id_ary.id_ary[i].rtn_func;
						timer_id_ary.id_ary[i].run_state = TIM_END;

						/*结束定时*/
						if (end_id.end_state == TIM_ONCE)
						{
							timer_id_ary.id_ary[i].id = ST_ID_NULL;
							timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
							timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)TIM_DESTROYED;
							timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)TIM_DESTROYED;
							timer_id_ary.id_ary[i].target = ST_END_COUNT_VALUE;
							timer_id_ary.id_ary[i].rtn_func = NULL;
						}
						/*循环触发*/
						if (end_id.end_state == TIM_CYCLE)
						{
							timer_id_ary.id_ary[i].count = ST_START_COUNT_VALUE;
							timer_id_ary.id_ary[i].run_state = end_id.run_state;
						}

						/*执行回调*/
						if (end_id.rtn_func != NULL)
						{
#ifdef ST_CLIENT_SERVER_MODE
							add_event_to_list(end_id.rtn_func, end_id.id);

#else
							end_id.rtn_func(end_id.id);
#endif
						}
					}
				}
			}
		}
	}
}

#ifdef ST_CLIENT_SERVER_MODE
/**
 * @brief 前台调用，处理对应id的事件
 * 后台中断将触发的timer id 标记
 * 此函数负责处理对应的id事件调用
 **/
void st_sorft_timer_event_prosess(void)
{
	uint16_t i = 0;
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if (event_id_ary.event_ary[i] != NULL)
		{
			event_id_ary.event_ary[i](event_id_ary.event_id[i]);
			event_id_ary.event_id[i] = ST_ID_NULL;
			event_id_ary.event_ary[i] = NULL;
			return;
		}
	}
}
#endif
#ifdef ST_CLIENT_SERVER_MODE
/**
 * @brief 添加事件回调到列表
 **/
static void add_event_to_list(st_rtn_func_t event_cb, uint16_t id)
{
	uint16_t i = 0;
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if (event_id_ary.event_ary[i] == NULL)
		{
			event_id_ary.event_ary[i] = event_cb;
			event_id_ary.event_id[i] = id;
			return;
		}
	}
}
#endif
/**
 @brief 定时器id赋值，使用定时器前先调用一次此接口
**/
void st_sorft_timer_init(void)
{
	uint16_t i = 0;
	/*  清空定时器所有 timer_id = 0*/
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		timer_id_ary.id_ary[i].id = ST_ID_NULL;
		timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
		timer_id_ary.id_ary[i].target = ST_END_COUNT_VALUE;
		timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)TIM_DESTROYED;
		timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)TIM_DESTROYED;
		timer_id_ary.id_ary[i].rtn_func = NULL;

#ifdef ST_CLIENT_SERVER_MODE
		event_id_ary.event_id[i] = ST_ID_NULL;
		event_id_ary.event_ary[i] = NULL;
#endif
	}
	timer_id_ary.timer_last_index = 0; /* 对管理的timer进行计数*/
	OPEN_COUNTER = 1;
}

/**
 @brief 启动一个定时器
*		timer_id 用户自定义ID
*		需要计数时间MS
*		计数状态，LOCK_TIMING(计数时无法刷新计数值，只能等到计数结束)
*		计数状态，TIMING 可以中途重新刷新计数值
**/

st_e_state_crat_t st_start_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state)
{
	uint16_t i = 0;
	uint8_t first_i = 0;
	uint8_t flag = ST_NULL_ID;

	if (timer_id == 0 || count == 0)
	{
		return TIM_CRAT_ERROR;
	}
	if (!OPEN_COUNTER)
	{
		st_sorft_timer_init();
	}
	/*************查找相同ID或者直接创建****************/
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if (timer_id_ary.id_ary[i].id == timer_id)
		{
			first_i = i;
			flag = ST_OLD_ID; // 查找相同ID
			if ((timer_id_ary.id_ary[i].run_state == TIM_LOCK) || (timer_id_ary.id_ary[i].run_state == TIM_END))
			{
				return (st_e_state_crat_t)TIM_LOCK;
			}
			break;
		}
		if ((timer_id_ary.id_ary[i].id == ST_ID_NULL) && (!flag))
		{
			first_i = i;
			flag = ST_NEW_ID; /* 查找并记录空闲ID*/
		}
	}
	if (!flag)
	{
		return TIM_CRAT_ERROR; /* 防止未查找到*/
	}
	if ((timer_id_ary.timer_last_index) < MAX_TIMER_NUM)
	{
		timer_id_ary.id_ary[first_i].id = timer_id;
		timer_id_ary.id_ary[first_i].count = ST_START_COUNT_VALUE; /* 计数初始值*/
		timer_id_ary.id_ary[first_i].target = count;
		timer_id_ary.id_ary[first_i].run_state = run_state;
		timer_id_ary.id_ary[first_i].end_state = (st_e_state_end_t)TIM_DESTROYED;
		timer_id_ary.id_ary[first_i].rtn_func = NULL;

		if (flag == ST_NEW_ID)
		{
			if (first_i >= MAX_USE_NUMBER)
			{
				MAX_USE_NUMBER = first_i + 1;
			}
			if (++timer_id_ary.timer_last_index >= MAX_USE_NUMBER) /* 新增一个ID ，进行+1计数 */
			{
				timer_id_ary.timer_last_index = MAX_USE_NUMBER;
			}
		}
		return TIM_CRAT_SUCES; /* 返回创建成功的状态 */
	}
	else
	{
		return TIM_CRAT_ERROR;
	}
}
/**
 *@brief 启动一次定时器
 * 计时结束自动销毁定时器
 * **/
st_e_state_crat_t st_start_once_timer(uint16_t timer_id, uint32_t count, st_e_state_run_t run_state)
{
	uint16_t i = 0;
	uint8_t first_i = 0;
	uint8_t flag = ST_NULL_ID;

	if (timer_id == 0 || count == 0)
	{
		return (st_e_state_crat_t)TIM_DESTROYED;
	}
	if (!OPEN_COUNTER)
	{
		st_sorft_timer_init();
	}
	/*************查找相同ID或者直接创建****************/
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if (timer_id_ary.id_ary[i].id == timer_id)
		{
			first_i = i;
			flag = ST_OLD_ID; /* 查找相同ID */
			if (timer_id_ary.id_ary[i].run_state == TIM_LOCK)
			{
				return (st_e_state_crat_t)TIM_LOCK;
			}
			break;
		}
		if ((timer_id_ary.id_ary[i].id == ST_ID_NULL) && (!flag))
		{
			first_i = i;
			flag = ST_NEW_ID;
		}
	}
	if (!flag)
	{
		return TIM_CRAT_ERROR;
	}
	if ((timer_id_ary.timer_last_index) < MAX_TIMER_NUM)
	{
		timer_id_ary.id_ary[first_i].id = timer_id;
		timer_id_ary.id_ary[first_i].count = ST_START_COUNT_VALUE;
		timer_id_ary.id_ary[first_i].target = count;
		timer_id_ary.id_ary[first_i].run_state = run_state;
		timer_id_ary.id_ary[first_i].end_state = TIM_ONCE;
		timer_id_ary.id_ary[first_i].rtn_func = NULL;
		if (flag == ST_NEW_ID)
		{
			if (first_i >= MAX_USE_NUMBER)
			{
				MAX_USE_NUMBER = first_i + 1;
			}
			if (++timer_id_ary.timer_last_index >= MAX_USE_NUMBER)
			{
				timer_id_ary.timer_last_index = MAX_USE_NUMBER;
			}
		}
		return TIM_CRAT_SUCES;
	}
	else
	{

		return TIM_CRAT_ERROR;
	}
}

/**
 * @brief 启动一次定时器
 * 计时结束自动销毁定时器
 **/
st_e_state_crat_t st_start_cb_timer(uint16_t timer_id,
									uint32_t count,
									st_e_state_run_t run_state,
									st_e_state_end_t end_state,
									st_rtn_func_t rtn_func)
{
	uint16_t i = 0;
	uint8_t first_i = 0;
	uint8_t flag = ST_NULL_ID;

	if (timer_id == 0 || count == 0 || rtn_func == NULL)
	{
		return (st_e_state_crat_t)TIM_DESTROYED;
	}
	if (!OPEN_COUNTER)
	{
		st_sorft_timer_init();
	}
	/*************查找相同ID或者直接创建****************/
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if (timer_id_ary.id_ary[i].id == timer_id)
		{
			first_i = i;
			flag = ST_OLD_ID;
			if (timer_id_ary.id_ary[i].run_state == TIM_LOCK)
			{
				return (st_e_state_crat_t)TIM_LOCK;
			}
			break;
		}
		if ((timer_id_ary.id_ary[i].id == ST_ID_NULL) && (!flag))
		{
			first_i = i;
			flag = ST_NEW_ID;
		}
	}
	if (!flag)
	{
		return TIM_CRAT_ERROR;
	}
	if ((timer_id_ary.timer_last_index) < MAX_TIMER_NUM)
	{
		timer_id_ary.id_ary[first_i].id = timer_id;
		timer_id_ary.id_ary[first_i].count = ST_START_COUNT_VALUE;
		timer_id_ary.id_ary[first_i].target = count;
		timer_id_ary.id_ary[first_i].run_state = run_state;
		timer_id_ary.id_ary[first_i].end_state = end_state;
		timer_id_ary.id_ary[first_i].rtn_func = rtn_func;

		if (flag == ST_NEW_ID)
		{
			if (first_i >= MAX_USE_NUMBER)
			{
				MAX_USE_NUMBER = first_i + 1;
			}
			if (++timer_id_ary.timer_last_index >= MAX_USE_NUMBER)
			{
				timer_id_ary.timer_last_index = MAX_USE_NUMBER;
			}
		}
		return TIM_CRAT_SUCES; /*返回创建成功的状态*/
	}
	else
	{

		return TIM_CRAT_ERROR;
	}
}

/**
 * @brief 关掉定时器，
 * TIM_SUSPEND 定时器不销毁，计数暂停
 * TIM_DESTROYED  销毁定时器，ID将被抹去
 */
st_e_state_close_t st_close_timer(uint16_t timer_id, st_e_state_close_t close_state)
{
	uint16_t i = 0;
	if (timer_id == 0)
	{
		return TIM_DESTROYED;
	}
	for (i = 0; i < MAX_USE_NUMBER; i++)
	{
		if (timer_id_ary.id_ary[i].id == timer_id)
		{
			break;
		}
	}
	if (i >= MAX_USE_NUMBER)
	{
		return TIM_DESTROYED;
	}
	if (close_state == TIM_SUSPEND)
	{ /* 暂停定时器,也就是关掉的意思 */
		/***暂停定时器****	查看是否有相同ID       	 ********/
		if ((timer_id_ary.id_ary[i].id) == timer_id)
		{
			timer_id_ary.id_ary[i].id = timer_id; /*ID不能抹去 */
			timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
			timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)TIM_SUSPEND; /*挂起定时器 */
			timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)TIM_SUSPEND;
			timer_id_ary.id_ary[i].target = ST_END_COUNT_VALUE;
			return TIM_SUSPEND;
		}
	}
	else if (close_state == TIM_DESTROYED)
	{ /* 销毁定时器 */
		/****销毁定时器***	查看是否有相同ID       	 ********/
		if ((timer_id_ary.id_ary[i].id) == timer_id)
		{
			timer_id_ary.id_ary[i].id = ST_ID_NULL;
			timer_id_ary.id_ary[i].count = ST_END_COUNT_VALUE;
			timer_id_ary.id_ary[i].run_state = (st_e_state_run_t)TIM_DESTROYED;
			timer_id_ary.id_ary[i].end_state = (st_e_state_end_t)TIM_DESTROYED;
			timer_id_ary.id_ary[i].target = ST_END_COUNT_VALUE;
			timer_id_ary.id_ary[i].rtn_func = NULL;

			if (timer_id_ary.timer_last_index > 0)
			{
				timer_id_ary.timer_last_index--;
			}

			if (timer_id_ary.id_ary[MAX_USE_NUMBER - 1].run_state == TIM_DESTROYED)
			{
				MAX_USE_NUMBER--;
			}
			return TIM_DESTROYED; // 返回刚刚创建的状态
		}
	}

	return TIM_DESTROYED;
}

/**
 *@bref 返回定时器状态
 有此ID的定时器就返回当前状态
 没有就返回DESTROYED
 * **/
st_e_state_run_t st_timer_state(uint16_t timer_id)
{
	uint16_t i = 0;
	if (timer_id == 0)
	{
		return (st_e_state_run_t)TIM_DESTROYED;
	}
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if ((timer_id_ary.id_ary[i].id) == timer_id)
		{
			return timer_id_ary.id_ary[i].run_state;
		}
	}
	return (st_e_state_run_t)TIM_DESTROYED;
}

/*
 @bref 查询计数值
*/
uint32_t st_timer_count_value(uint16_t timer_id)
{
	uint16_t i = 0;
	if (timer_id == 0)
	{
		return ST_END_COUNT_VALUE;
	}
	for (i = 0; i < MAX_TIMER_NUM; i++)
	{
		if ((timer_id_ary.id_ary[i].id) == timer_id)
		{
			return timer_id_ary.id_ary[i].count;
		}
	}
	return ST_END_COUNT_VALUE;
}

